package com.tencent.wxcloudrun.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/donghuan")
public class DongHuanController {
    // 获取客户端的 IP 地址
    private String getClientIp(HttpServletRequest request) {
        String ip = request.getHeader("X-Forwarded-For");
        if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }

    @GetMapping("/dcim/dcim.asmx/GetSite")
    public String getSite(HttpServletResponse response, HttpServletRequest request) {

        // 获取访问者的 IP 地址
        String ipAddress = getClientIp(request);

        // 获取用户代理 (浏览器信息)
        String userAgent = request.getHeader("User-Agent");

        // 来源页面
        String referer = request.getHeader("Referer");
        // 打印日志（调试用）
        System.out.println("请求的 IP:  " + ipAddress);
        System.out.println("请求的 来源页面: " + referer);
        System.out.println("请求的 User-Agent: " + userAgent);

        response.setContentType("application/xml;charset=UTF-8");
        return "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                "<string xmlns=\"http://tempuri.org/\">[{\"id\": 1,\"ParentID\": 0,\"SubClass\": 0,\"IP\": \"\",\"Name\":\"常州税务\",\"NetStatus\":1, \"SUBTYPE\": \"1\"},{\"id\": 3,\"ParentID\": 1,\"SubClass\": 0,\"IP\": \"\",\"Name\":\"龙锦路机房\",\"NetStatus\":1, \"SUBTYPE\": \"1\"},{\"id\": 4,\"ParentID\": 1,\"SubClass\": 0,\"IP\": \"\",\"Name\":\"河海路机房\",\"NetStatus\":1, \"SUBTYPE\": \"1\"},{\"id\": 5,\"ParentID\": 4,\"SubClass\": 0,\"IP\": \"77.32.23.67:10002:42\",\"Name\":\"电表2\",\"NetStatus\":1, \"SUBTYPE\": \"10\"},{\"id\": 333,\"ParentID\": 4,\"SubClass\": 0,\"IP\": \"77.32.23.71\",\"Name\":\"空调1\",\"NetStatus\":1, \"SUBTYPE\": \"9\"},{\"id\": 335,\"ParentID\": 4,\"SubClass\": 0,\"IP\": \"77.32.23.72\",\"Name\":\"空调2\",\"NetStatus\":1, \"SUBTYPE\": \"9\"},{\"id\": 337,\"ParentID\": 3,\"SubClass\": 0,\"IP\": \"141.32.16.140:10005:3\",\"Name\":\"小型机房电表3\",\"NetStatus\":1, \"SUBTYPE\": \"10\"},{\"id\": 27,\"ParentID\": 4,\"SubClass\": 0,\"IP\": \"77.32.23.67:10001:2\",\"Name\":\"电表1\",\"NetStatus\":1, \"SUBTYPE\": \"10\"},{\"id\": 62,\"ParentID\": 4,\"SubClass\": 0,\"IP\": \"77.32.23.70:10001\",\"Name\":\"新山特UPS1\",\"NetStatus\":1, \"SUBTYPE\": \"8\"},{\"id\": 117,\"ParentID\": 4,\"SubClass\": 0,\"IP\": \"77.32.23.70:10002\",\"Name\":\"新山特UPS2\",\"NetStatus\":1, \"SUBTYPE\": \"8\"},{\"id\": 172,\"ParentID\": 4,\"SubClass\": 0,\"IP\": \"77.32.23.70:502\",\"Name\":\"采集机\",\"NetStatus\":1, \"SUBTYPE\": \"11\"},{\"id\": 180,\"ParentID\": 4,\"SubClass\": 0,\"IP\": \"77.32.23.66:10001\",\"Name\":\"山特UPS\",\"NetStatus\":1, \"SUBTYPE\": \"8\"},{\"id\": 200,\"ParentID\": 4,\"SubClass\": 0,\"IP\": \"77.32.23.68:10001\",\"Name\":\"温湿度\",\"NetStatus\":1, \"SUBTYPE\": \"17\"},{\"id\": 226,\"ParentID\": 4,\"SubClass\": 0,\"IP\": \"77.32.23.68:10002\",\"Name\":\"空调3\",\"NetStatus\":1, \"SUBTYPE\": \"9\"},{\"id\": 362,\"ParentID\": 3,\"SubClass\": 0,\"IP\": \"141.32.16.140:10005:4\",\"Name\":\"小型机房电表4\",\"NetStatus\":1, \"SUBTYPE\": \"10\"},{\"id\": 387,\"ParentID\": 3,\"SubClass\": 0,\"IP\": \"141.32.16.140:10005:5\",\"Name\":\"小型机房电表5\",\"NetStatus\":1, \"SUBTYPE\": \"10\"},{\"id\": 412,\"ParentID\": 3,\"SubClass\": 0,\"IP\": \"141.32.16.140:10005:6\",\"Name\":\"网络机房电表6\",\"NetStatus\":1, \"SUBTYPE\": \"10\"},{\"id\": 437,\"ParentID\": 3,\"SubClass\": 0,\"IP\": \"141.32.16.140:10005:7\",\"Name\":\"网络机房电表7\",\"NetStatus\":1, \"SUBTYPE\": \"11\"},{\"id\": 462,\"ParentID\": 3,\"SubClass\": 0,\"IP\": \"141.32.16.140:10005:8\",\"Name\":\"配电房电表8\",\"NetStatus\":1, \"SUBTYPE\": \"10\"},{\"id\": 487,\"ParentID\": 3,\"SubClass\": 0,\"IP\": \"141.32.16.140:10005:9\",\"Name\":\"配电房电表9\",\"NetStatus\":1, \"SUBTYPE\": \"10\"},{\"id\": 512,\"ParentID\": 3,\"SubClass\": 0,\"IP\": \"141.32.16.140:10007:3\",\"Name\":\"网络机房空调1\",\"NetStatus\":1, \"SUBTYPE\": \"9\"},{\"id\": 558,\"ParentID\": 3,\"SubClass\": 0,\"IP\": \"141.32.16.140:10007:4\",\"Name\":\"网络机房空调2\",\"NetStatus\":1, \"SUBTYPE\": \"9\"},{\"id\": 604,\"ParentID\": 3,\"SubClass\": 0,\"IP\": \"141.32.16.141:10001\",\"Name\":\"温湿度\",\"NetStatus\":1, \"SUBTYPE\": \"17\"},{\"id\": 626,\"ParentID\": 3,\"SubClass\": 0,\"IP\": \"141.32.16.140:502\",\"Name\":\"采集机\",\"NetStatus\":1, \"SUBTYPE\": \"11\"},{\"id\": 629,\"ParentID\": 3,\"SubClass\": 0,\"IP\": \"141.32.16.140:10004:1\",\"Name\":\"服务器机房空调1\",\"NetStatus\":1, \"SUBTYPE\": \"9\"},{\"id\": 761,\"ParentID\": 3,\"SubClass\": 0,\"IP\": \"141.32.16.140:10004:2\",\"Name\":\"服务器机房空调2\",\"NetStatus\":1, \"SUBTYPE\": \"9\"},{\"id\": 893,\"ParentID\": 3,\"SubClass\": 0,\"IP\": \"141.32.16.140:10001\",\"Name\":\"UPS1\",\"NetStatus\":1, \"SUBTYPE\": \"8\"},{\"id\": 944,\"ParentID\": 3,\"SubClass\": 0,\"IP\": \"141.32.16.140:10002\",\"Name\":\"UPS2\",\"NetStatus\":1, \"SUBTYPE\": \"8\"},{\"id\": 996,\"ParentID\": 3,\"SubClass\": 0,\"IP\": \"141.32.16.140:10008\",\"Name\":\"氢气\",\"NetStatus\":1, \"SUBTYPE\": \"11\"},{\"id\": 1326,\"ParentID\": 1,\"SubClass\": 0,\"IP\": \"\",\"Name\":\"河海路西大厅1楼机房\",\"NetStatus\":1, \"SUBTYPE\": \"1\"},{\"id\": 1327,\"ParentID\": 1326,\"SubClass\": 0,\"IP\": \"77.32.21.175:10001\",\"Name\":\"电表\",\"NetStatus\":1, \"SUBTYPE\": \"10\"},{\"id\": 1364,\"ParentID\": 1326,\"SubClass\": 0,\"IP\": \"77.32.21.175:10002\",\"Name\":\"UPS\",\"NetStatus\":1, \"SUBTYPE\": \"8\"},{\"id\": 1549,\"ParentID\": 1326,\"SubClass\": 0,\"IP\": \"77.32.21.175:10003\",\"Name\":\"温湿度1\",\"NetStatus\":1, \"SUBTYPE\": \"17\"},{\"id\": 1553,\"ParentID\": 1326,\"SubClass\": 0,\"IP\": \"77.32.21.175:10004\",\"Name\":\"温湿度2\",\"NetStatus\":1, \"SUBTYPE\": \"17\"},{\"id\": 1557,\"ParentID\": 1326,\"SubClass\": 0,\"IP\": \"77.32.21.175:502\",\"Name\":\"采集机\",\"NetStatus\":1, \"SUBTYPE\": \"11\"}]</string>";
    }
    @GetMapping("/dcim/dcim.asmx/GetDataForIP")
    public String getSite(@RequestParam("ip") String ip,HttpServletResponse response) {
        System.out.println("访问的ip  " + ip);
        response.setContentType("application/xml;charset=UTF-8");
        return "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                "<string xmlns=\"http://tempuri.org/\">[{\"IP\": \"77.32.23.68:10001\",\"subName\":\"温度1\",\"WYID\":\"77.32.23.68:10001_1\",\"value\":\"22.40\", \"dw\": \"℃\", \"SUBID\": 1, \"TIME\": \"2024-11-21 16:14:09\"},{\"IP\": \"77.32.23.68:10001\",\"subName\":\"湿度1\",\"WYID\":\"77.32.23.68:10001_2\",\"value\":\"42.20\", \"dw\": \"%\", \"SUBID\": 2, \"TIME\": \"2024-11-21 16:14:09\"},{\"IP\": \"77.32.23.68:10001\",\"subName\":\"温度2\",\"WYID\":\"77.32.23.68:10001_3\",\"value\":\"20.40\", \"dw\": \"℃\", \"SUBID\": 3, \"TIME\": \"2024-11-21 16:14:09\"},{\"IP\": \"77.32.23.68:10001\",\"subName\":\"湿度2\",\"WYID\":\"77.32.23.68:10001_4\",\"value\":\"52.80\", \"dw\": \"%\", \"SUBID\": 4, \"TIME\": \"2024-11-21 16:14:09\"},{\"IP\": \"77.32.23.68:10001\",\"subName\":\"温度3\",\"WYID\":\"77.32.23.68:10001_5\",\"value\":\"21.30\", \"dw\": \"℃\", \"SUBID\": 5, \"TIME\": \"2024-11-21 16:14:09\"},{\"IP\": \"77.32.23.68:10001\",\"subName\":\"湿度3\",\"WYID\":\"77.32.23.68:10001_6\",\"value\":\"45.30\", \"dw\": \"%\", \"SUBID\": 6, \"TIME\": \"2024-11-21 16:14:09\"},{\"IP\": \"77.32.23.68:10001\",\"subName\":\"温度4\",\"WYID\":\"77.32.23.68:10001_7\",\"value\":\"23.40\", \"dw\": \"℃\", \"SUBID\": 7, \"TIME\": \"2024-11-21 16:14:09\"},{\"IP\": \"77.32.23.68:10001\",\"subName\":\"湿度4\",\"WYID\":\"77.32.23.68:10001_8\",\"value\":\"39.80\", \"dw\": \"%\", \"SUBID\": 8, \"TIME\": \"2024-11-21 16:14:09\"},{\"IP\": \"77.32.23.68:10001\",\"subName\":\"温度5\",\"WYID\":\"77.32.23.68:10001_9\",\"value\":\"24.60\", \"dw\": \"℃\", \"SUBID\": 9, \"TIME\": \"2024-11-21 16:14:09\"},{\"IP\": \"77.32.23.68:10001\",\"subName\":\"湿度5\",\"WYID\":\"77.32.23.68:10001_10\",\"value\":\"36.90\", \"dw\": \"%\", \"SUBID\": 10, \"TIME\": \"2024-11-21 16:14:09\"},{\"IP\": \"77.32.23.68:10001\",\"subName\":\"温度6\",\"WYID\":\"77.32.23.68:10001_11\",\"value\":\"21.50\", \"dw\": \"℃\", \"SUBID\": 11, \"TIME\": \"2024-11-21 16:14:09\"},{\"IP\": \"77.32.23.68:10001\",\"subName\":\"湿度6\",\"WYID\":\"77.32.23.68:10001_12\",\"value\":\"44.90\", \"dw\": \"%\", \"SUBID\": 12, \"TIME\": \"2024-11-21 16:14:09\"},{\"IP\": \"77.32.23.68:10001\",\"subName\":\"温度7\",\"WYID\":\"77.32.23.68:10001_13\",\"value\":\"21.00\", \"dw\": \"℃\", \"SUBID\": 13, \"TIME\": \"2024-11-21 16:14:09\"},{\"IP\": \"77.32.23.68:10001\",\"subName\":\"湿度7\",\"WYID\":\"77.32.23.68:10001_14\",\"value\":\"49.90\", \"dw\": \"%\", \"SUBID\": 14, \"TIME\": \"2024-11-21 16:14:09\"},{\"IP\": \"77.32.23.68:10001\",\"subName\":\"温度8\",\"WYID\":\"77.32.23.68:10001_15\",\"value\":\"20.70\", \"dw\": \"℃\", \"SUBID\": 15, \"TIME\": \"2024-11-21 16:14:09\"},{\"IP\": \"77.32.23.68:10001\",\"subName\":\"湿度8\",\"WYID\":\"77.32.23.68:10001_16\",\"value\":\"52.20\", \"dw\": \"%\", \"SUBID\": 16, \"TIME\": \"2024-11-21 16:14:09\"},{\"IP\": \"77.32.23.68:10001\",\"subName\":\"温度9\",\"WYID\":\"77.32.23.68:10001_17\",\"value\":\"23.60\", \"dw\": \"℃\", \"SUBID\": 17, \"TIME\": \"2024-11-21 16:14:09\"},{\"IP\": \"77.32.23.68:10001\",\"subName\":\"湿度9\",\"WYID\":\"77.32.23.68:10001_18\",\"value\":\"38.90\", \"dw\": \"%\", \"SUBID\": 18, \"TIME\": \"2024-11-21 16:14:09\"},{\"IP\": \"77.32.23.68:10001\",\"subName\":\"温度10\",\"WYID\":\"77.32.23.68:10001_19\",\"value\":\"23.30\", \"dw\": \"℃\", \"SUBID\": 19, \"TIME\": \"2024-11-21 16:14:09\"},{\"IP\": \"77.32.23.68:10001\",\"subName\":\"湿度10\",\"WYID\":\"77.32.23.68:10001_20\",\"value\":\"39.20\", \"dw\": \"%\", \"SUBID\": 20, \"TIME\": \"2024-11-21 16:14:09\"},{\"IP\": \"77.32.23.68:10001\",\"subName\":\"温度11\",\"WYID\":\"77.32.23.68:10001_21\",\"value\":\"22.50\", \"dw\": \"℃\", \"SUBID\": 21, \"TIME\": \"2024-11-21 16:14:09\"},{\"IP\": \"77.32.23.68:10001\",\"subName\":\"湿度11\",\"WYID\":\"77.32.23.68:10001_22\",\"value\":\"41.40\", \"dw\": \"%\", \"SUBID\": 22, \"TIME\": \"2024-11-21 16:14:09\"},{\"IP\": \"77.32.23.68:10001\",\"subName\":\"电池间温度\",\"WYID\":\"77.32.23.68:10001_23\",\"value\":\"19.60\", \"dw\": \"℃\", \"SUBID\": 23, \"TIME\": \"2024-11-21 16:14:09\"},{\"IP\": \"77.32.23.68:10001\",\"subName\":\"电池间湿度\",\"WYID\":\"77.32.23.68:10001_24\",\"value\":\"50.50\", \"dw\": \"%\", \"SUBID\": 24, \"TIME\": \"2024-11-21 16:14:09\"}]</string>";
    }

}
